package com.cuitrip.business.bill;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.cuitrip.apiservice.UserBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.app.UserConfig;
import com.cuitrip.app.base.UnitUtils;
import com.cuitrip.component.choicedialog.ChoiceDialog;
import com.cuitrip.service.R;
import com.cuitrip.util.PlatformUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.proxy.ApiProxy;
import com.lab.network.proxy.ApiProxyBuilder;
import com.lab.network.proxy.ApiProxyCallBack;
import com.lab.utils.LogHelper;
import com.lab.utils.MessageUtils;
import com.lab.utils.NumberUtils;
import com.lab.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by baziii on 15/8/18.
 */
public class BillCashActivity extends CustomUiFragmentActivity {

    public static final String MOENY_KEY = "MOENY_KEY";
    public static final String MOENY_TYPE_KEY = "MOENY_TYPE_KEY";
    public static final int REQUEST_CODE_EDIT_PAYPAL_ACCOUNT = 1;
    @Bind(R.id.amount)
    TextView amount;
    @Bind(R.id.account)
    TextView account;
    @Bind(R.id.money)
    TextView money;
    @Bind(R.id.bill_money_type)
    TextView billMoneyType;
    String avaliableMoney;
    String avaliableMoneyType;

    public static void start(Activity activity, String money, String moneytype,int code) {
        activity.startActivityForResult(new Intent(activity, BillCashActivity.class).putExtra(MOENY_KEY, money)
                .putExtra(MOENY_TYPE_KEY, moneytype),code);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.ct_get_cash);
        ButterKnife.bind(this);
        money.setHint("0.00");
        avaliableMoney = getIntent().getStringExtra(MOENY_KEY);
        avaliableMoneyType = getIntent().getStringExtra(MOENY_TYPE_KEY);
        render();
        account.setText(UserConfig.getPaypalAccount());
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName=getString(R.string.withdraw);
        return config;
    }

    public void render() {
        String accountValue=UserConfig.getPaypalAccount();
        if (!TextUtils.isEmpty(accountValue)){
            account.setText(UserConfig.getPaypalAccount());
        }
        amount.setText(NumberUtils.getMoneyFormat(avaliableMoney));
        billMoneyType.setText(avaliableMoneyType.toUpperCase());
    }

    @OnClick(R.id.balanceExplain)
    public void showBalanceExplain() {
        ExplainActivity.startActivityBy(this, ExplainActivity.SHOW_INFO_ABOUT_BALANCE);
    }

    @OnClick(R.id.ratingToday)
    public void showRating() {
        ExplainActivity.startActivityBy(this, ExplainActivity.SHOW_INFO_ABOUT_RATING);
    }

    @OnClick(R.id.submit)
    public void trySubmit() {
        String accountValue=account.getText().toString();
        String moneyValue=money.getText().toString();
        if (TextUtils.isEmpty(accountValue)) {
            Utils.shakeView(findViewById(R.id.layoutAccount));
            return;
        }
        if (TextUtils.isEmpty(moneyValue)) {
            Utils.shakeView(findViewById(R.id.layoutMoney));
            return;
        }
        ChoiceDialog choiceDialog=ChoiceDialog.getInstance();
        choiceDialog.setDialogTitleDividerVisibility(false);
        choiceDialog.setDialogMessageDividerVisibility(true);
        choiceDialog.setBottomDivideVisiblity(true);
        choiceDialog.setDialogTitle(getString(R.string.operation_submit));
        choiceDialog.setDialogMessage(getString(R.string.account_paypal_withdraw_confirm, moneyValue, accountValue));
        choiceDialog.setDialogCoupleStyleSetting(
                getResources().getString(R.string.operation_yes),
                getResources().getString(R.string.operation_cancel), new ChoiceDialog.DialogStyleCoupleCallback() {
            @Override
            public boolean onPositiveButtonClick() {
                submit();
                return false;
            }

            @Override
            public boolean onNegativeButtonClick() {
                return false;
            }
        });
        showDialogFragment(choiceDialog);
    }

    public void submit() {
        showLoading();
        UserConfig.setPaypalAccount(account.getText().toString());
        ApiProxy apiProxy=ApiProxyBuilder.getBuilder().addApiCallBack(ApiManager.GET_CASH,callBack).build();
        UserBusiness.getCash(apiProxy , account.getText().toString(), money.getText().toString(), UnitUtils.getCashType());
    }
    IProxyCallback callBack =new ApiProxyCallBack(){
            @Override
            public boolean onSuccess(Object data, CtApiResponse response) {
                LogHelper.e("omg", "suc " + String.valueOf(response.result));
                MessageUtils.showToast(getString(R.string.feedback_submit_suc));
                finish();
                hideLoading();
                return super.onSuccess(data, response);
            }

            @Override
            public boolean onFaild(CtApiResponse response) {
                LogHelper.e("omg", "failed ");
                String msg;
                if (response != null && !TextUtils.isEmpty(response.msg)) {
                    msg = response.msg;
                } else {
                    msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                }
                MessageUtils.showToast(msg);
                hideLoading();
                return super.onFaild(response);
            }
    };

    @OnClick(R.id.layoutAccount)
    public void setAccount() {
        String value=account.getText().toString();
        BindPayPalActivity.startBindActivityForResult(this, value, REQUEST_CODE_EDIT_PAYPAL_ACCOUNT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String result = data.getStringExtra(BindPayPalActivity.KEY_RESULT);
            switch (requestCode) {
                case REQUEST_CODE_EDIT_PAYPAL_ACCOUNT:
                    account.setText(result);
                    break;
            }
        }
    }
}
